package com.dhcc.sdc.cdss.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.dto.SdcCdssItemStageDicDto;
import com.dhcc.sdc.base.service.ISdcStageRelationService;
import com.dhcc.sdc.cdss.dao.SdcCdssItemStageDicMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssItemStageDicEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssItemStageEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssItemStageDicService;
import com.dhcc.sds.base.service.ISdsCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 诊疗阶段表数据服务层接口
 * 
 * @ClassName: SdcCdssItemStageDicServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2023-04-06 10:25:59
 */
@Service("sdcCdssItemStageDicService")
public class SdcCdssItemStageDicServiceImpl extends BaseServiceImpl<SdcCdssItemStageDicMapper, SdcCdssItemStageDicEntity> implements ISdcCdssItemStageDicService {

	@Autowired
	private ISdsCommonService sdsCommonService;
	@Autowired
	private ISdcStageRelationService sdcStageRelationService;

	@Override
	public List<SdcCdssItemStageDicEntity> page(Page<SdcCdssItemStageDicEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcCdssItemStageDicEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public JSONObject getStage() {
		JSONObject jsObj = new JSONObject();
		jsObj.put("","");
		List<SdcCdssItemStageDicEntity> sdcCdssItemStageDicEntitys = this.baseMapper.selectListGroupByName(null);
		for ( SdcCdssItemStageDicEntity sdcCdssItemStageDicEntity : sdcCdssItemStageDicEntitys){
			jsObj.put(sdcCdssItemStageDicEntity.getStageName(), sdcCdssItemStageDicEntity.getStageName());
		}
		return jsObj;
	}

	@Override
	public List<SdcCdssItemStageDicDto> stagePage(Page<SdcCdssItemStageDicDto> page, Map<String, Object> map) {
		List<SdcCdssItemStageDicDto> result = this.baseMapper.stagePage(page, map);
		// 获取科室名称
		JSONObject departMap = sdsCommonService.findDeptList();
		result.forEach(sdcCdssItemStageDicDto -> {
			String linkDeptStr = sdcCdssItemStageDicDto.getLinkDept();
			if (CommonUtil.isEmpty(linkDeptStr)) return;
			StringBuilder linkDeptName = new StringBuilder();
			String[] linkDepts = linkDeptStr.split(",");
			for (String linkDept : linkDepts) {
				if (CommonUtil.isEmpty(linkDept)) continue;
				if (CommonUtil.isEmpty(linkDeptName.toString())) {
					linkDeptName = new StringBuilder(departMap.getString(linkDept));
				} else {
					linkDeptName.append(",").append(departMap.getString(linkDept));
				}
			}
			sdcCdssItemStageDicDto.setLinkDeptName(linkDeptName.toString());
		});

		return result;
	}

	@Override
	public void delStageById(Long sdcCdssItemStageDicId) {
		Map<String,Object> map = new HashMap<>();
		map.put("stage_id", sdcCdssItemStageDicId);
		sdcStageRelationService.deleteByMap(map);
		this.baseMapper.deleteById(sdcCdssItemStageDicId);
	}

	@Override
	public Boolean updateCheck(SdcCdssItemStageDicEntity sdcCdssItemStageDic) {
		Wrapper<SdcCdssItemStageEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("stage_id", sdcCdssItemStageDic.getId());
		int count = sdcStageRelationService.selectCount(wrapper);
		SdcCdssItemStageDicEntity sdcCdssItemStageDicEntity = this.baseMapper.selectById(sdcCdssItemStageDic.getId());
		if (count != 0 && sdcCdssItemStageDicEntity != null && !sdcCdssItemStageDicEntity.getEntityId().equals(sdcCdssItemStageDic.getEntityId())) {
			return false;
		}
		return true;
	}

}